//
// Created by cjl on 2019-01-16.
//

#include <stdio.h>
#include "log.h"
#include <libavformat/avformat.h>

int split(const char *src_path, const char *dst_path, const int start_sec, const int end_sec);

// 视频分割
int main() {
    char *src = "/Users/caijinglong/code/c/demos/ffmpeg/log/src.mp4";
    char *dst = "/Users/caijinglong/code/c/demos/ffmpeg/log/split-1.mp4";
    av_register_all();
    av_log_set_level(AV_LOG_DEBUG);
    return split(src, dst, 10, 20);
}

int split(const char *src_path, const char *dst_path, const int start_sec, const int end_sec) {
    //定义一个
    int ret = 0, error_code = -1;
    AVFormatContext *in_ctx = NULL, *out_ctx = NULL;
    LOGD("准备开始\n");

    ret = avformat_open_input(&in_ctx, src_path, NULL, NULL);

    if (ret < 0) {
        error_code = ret;
        goto error_end;
    }

    av_dump_format(in_ctx, 0, src_path, 0);

    /// 收集流信息

//    ret = avformat_find_stream_info(in_ctx, NULL);
//    if (ret < 0) {
//        error_code = ret;
//        goto error_end;
//    }

    LOGD("stream count = %d\n", in_ctx->nb_streams);

    error_end:
    if (error_code != -1) {
        LOGE("error : %s\n", av_err2str(error_code));
    }

    end:

    if (in_ctx) {
        avformat_free_context(in_ctx);
    }

    if (out_ctx) {
        avformat_free_context(out_ctx);
    }

    return ret;
}